package org.yccloud.yc.demo.kt.service

import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.stereotype.Service
import org.yccloud.yc.demo.kt.model.Message
import java.util.*

@Service
class MessageService(val db: JdbcTemplate) {

    fun findMessage(): List<Message> = db.query("select * from message") { response, _ ->
        Message(
            response.getString("id"),
            response.getString("text"),
        )
    }

    fun save(message: Message) {
        val id = message.id ?: UUID.randomUUID().toString();
        db.update(
            "insert into message values(?,?)",
            id, message.text
        )
    }
}